package pl.makimo.ksr.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import pl.makimo.data.WeatherRecord;
import pl.makimo.fuzzy.linguistics.DiscreteLinguisticVariable;
import pl.makimo.fuzzy.sets.CrispSet;
import pl.makimo.fuzzy.sets.FuzzySet;

public class LinguisticVariablePanel extends JPanel {
	public DiscreteLinguisticVariable<WeatherRecord> variable;
	public CrispSet<WeatherRecord> set;
	public String name;
	
	public JList nameList;
	public DefaultListModel listModel;
	
	public void addFuzzySet(FuzzySet<WeatherRecord> set, String name) {
		listModel.addElement(name);
		variable.add(name, set);
		
		System.out.println(set);
		System.out.println(name);
	}
	
	public JButton createFuzzySetButton(final Class<?> fuzzysetCreator, String title) {
		JButton b1 = new JButton(title);
	    b1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {				
				try {
					final FuzzySetCreator creator = (FuzzySetCreator)fuzzysetCreator.newInstance();
					creator.setAction(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							addFuzzySet(creator.getFuzzySet(), creator.getName());
						}
					});
					creator.show();					
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});		
	    
	    return b1;
	}		
	
	/**
	 * Create the frame.
	 */
	public LinguisticVariablePanel(String name, CrispSet<WeatherRecord> list) {
		this.name = name;
		this.set = list;
		
		this.variable = new DiscreteLinguisticVariable<WeatherRecord>(name, list);
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		this.add(new JLabel("Zmienna: " + name));
		
		listModel = new DefaultListModel();
		nameList = new JList(listModel);
		nameList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		nameList.setVisibleRowCount(-1);
		
		JScrollPane listScroller = new JScrollPane(nameList);
		listScroller.setPreferredSize(new Dimension(250, 80));		
	
		this.add(listScroller);
		
		this.add(createFuzzySetButton(DiscreteType1FuzzySetCreator.class, "Type-1 fuzzy set"));
		this.add(createFuzzySetButton(DiscreteType2FuzzySetCreator.class, "Type-2 fuzzy set"));
	}
}
